let moduleData = require('./module/data')
let reqList = Object.keys(moduleData);

//导入http模块
const HTTP = require("http");

//创建web服务器实例
const SERVER = HTTP.createServer();

//为服务器绑定request事件
//req 请求对象
//res 响应对象
SERVER.on("request", (req, res) => {
  res.setHeader("Content-Type", "text/html;charset=utf-8");
  console.log("本地服务被访问了！！！！");
  let data = "<h1>404 Not Found</h1>";
  //设置响应头以及编码格式
  
  reqList.forEach((reqItem) => {
    if (req.url == `/${reqItem}`) {
      console.log(`访问URL地址： ${req.url}  \nmethod类型： ${req.method}`);
      data = moduleData[reqItem];
    }
  });
  res.end(JSON.stringify(data));
});

//启动服务器
let port = 8083
SERVER.listen(port, () => {
  console.log(`http server running at http://127.0.0.1:${port}`);
});


